package no.ntnu.fp.gui;

import java.awt.Color;
import java.sql.Date;
import java.sql.Time;
import java.util.List;


import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.text.html.parser.DTD;


import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.CalendarHelper;
import no.ntnu.fp.model.Person;


public class EventPanel extends JPanel {
	
	
	//private String apoointmentTitle, startTime, endTime, place, date;
	//private Appointment nextAppointment;
	
	public EventPanel() {
		
		setBorder(new LineBorder(Color.LIGHT_GRAY));

		setBackground(Color.WHITE);
		setBounds(131, 0, 520, 92);
		Appointment nextApp = getNextAppointment();
		JLabel lblNewLabel = new JLabel(
				//"<html>\r\n<p>Neste avtale: </p>\r\n<p>Fredag 18 Mars</p>\r\n<p>10:00 - 12:00 </p>\r\n</html>"
				"<html>\r\n<p>Neste avtale : </p>\r\n<p>" + getStartDateForNextAppointment(nextApp)+"</p>\r\n<p> " + getStartTimeForNextAppointment(nextApp) + "</p>\r\n</html>"
		);
		lblNewLabel.setBounds(7, 23, 88, 62);
		add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel(
				//"<html>\r\n<p>Morgenm\u00F8te med gjengen</p>\r\n<p> &#32;</p>\r\n<p> &#32;</p>\r\n</html>"
				"<html>\r\n<p>"+getTitleForNextAppointment(nextApp)+"</p>\r\n<p> &#32;</p>\r\n<p> &#32;</p>\r\n</html>"

				//getTitleForNextAppointment(getNextAppointment())
		);
		lblNewLabel_1.setBounds(95, 23, 155, 63);
		add(lblNewLabel_1);

		JLabel lblBeskrivelse = new JLabel(
				//"<html>\r\n<p>Beskrivelse: Morgen m\u00F8te med gjengen. Vi m\u00E5 fullf\u00F8re sm\u00E5 tr\u00E5der i prosjektet, samt planlegge festen p\u00E5 kvelden! (og spise boller og drikke kaffe)</p>\r\n</html>"
				"<html>\r\n<p>" + getPlaceForNextAppointment(nextApp) + "</p>\r\n</html>"
		);
		lblBeskrivelse.setBounds(262, 24, 252, 62);
		add(lblBeskrivelse);
	}
	public Appointment getNextAppointment(){
		Person p = Person.getCurrentUser();
		System.out.println(p.getName());
		CalendarHelper ca = new CalendarHelper();
		Date dnow = ca.getFirstCalendarDayDate(), tempDate  = null;
		Time tnow = ca.getDateTime(), tempTime = null;
		
		List<Appointment> liste = Appointment.getAppointmentsFor(p);
		if(liste.isEmpty()){
			return null;
		}
		Appointment a = null, next = liste.get(0);
		/*
		 * set default time
		 */
		
		
		/*
		 * 
		 
		for (Appointment appointment : liste) {
			tempDate = appointment.getStartDate();
			tempTime = appointment.getStart();
			if(tempDate.after(dnow)||(tempTime.after(tnow)&&tempDate.equals(dnow))){
				break;
			}
		}
		*/
//		System.out.println("TempDate" + tempDate.toString());
//		System.out.println("TempTime" + tempTime.toString());
//		if(tempDate == null || tempTime == null){
//			return null;
//		}
			
		tempTime = liste.get(0).getStart();
		tempDate = liste.get(0).getStartDate();
		//System.out.println(next.getTitle());
		for(Appointment apps : liste){
			
			Date d = apps.getStartDate();
			Time t = apps.getStart();
			//System.out.println("DNOW: " + dnow.toString());
			if((d.before(tempDate) ||(d.equals(tempDate)&&t.before(tempTime)))
				|| (d.equals(dnow) && t.after(tnow) && t.before(tempDate))){
				if(d.after(dnow)){
					tempDate = apps.getStartDate();
					tempTime = apps.getStart();
					next = apps;
				}
			}
		
		}
		System.out.println(next.getTitle());
		System.out.println(next.getId());
		
		return next;
		
	}
	public String getTitleForNextAppointment(Appointment a){
		return a==null ? "Du har ingen kommende avtaler" : a.getTitle();
	}
	public String getStartTimeForNextAppointment(Appointment a){
		return (a == null || a.getStart()==null)? "" : a.getStart().toString();
	}
	public String getStartDateForNextAppointment(Appointment a){
		return (a == null || a.getStartDate()==null )? "" : a.getStartDate().toString();
	}
	public String getPlaceForNextAppointment(Appointment a){
		return (a == null||a.getRoom()==null) ? "" : a.getRoom().toString();
	}
	
}
